<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

defined('ANS') or die();

$Config->load('languages.php', 'scene', 'scene_');

$language = $Vars->get('upload_language');
$before = str_replace(array('..', '/', '\\'), '', $Vars->get('upload_file'));

if (!in_array($language, $Config->scene_languages['availables'])) {
	$Vars->message(__('No valid language was selected.'), 'ko');

	return false;
}

$file = $Vars->get('file');

if (!is_array($file) || !is_file($file['tmp_name']) || !$file['size']) {
	$Vars->message(__('No valid language file was upload.'), 'ko');

	return false;
}

$cmd = trim(shell_exec('which msgfmt'));

if (empty($cmd)) {
	$Vars->message(__('Command msgfmt does not exists in this server. Do you have installed gettext tools?'), 'ko');

	return false;
}

$dir = filePath('scene/languages|'.$language);
$old_file = $dir.'/'.$before.'.mo';

if (!is_dir($dir)) {
	if (!is_writable(dirname($dir))) {
		$Vars->message(__('Language folder has not writing permissions. Language file could be not uploaded.'), 'ko');

		return false;
	}

	mkdir($dir, 0755);
}

if (!is_writable($dir)) {
	$Vars->message(__('Language folder has not writing permissions. Language file could be not uploaded.'), 'ko');

	return false;
}

if (is_file($old_file) && !is_writable($old_file)) {
	$Vars->message(__('Previous language file has not writing permissions. Language file could be not uploaded.'), 'ko');

	return false;
}

$execute = $cmd.' -c -v -o "'.$old_file.'" "'.$file['tmp_name'].'"';
$output = shell_exec($execute);

move_uploaded_file($file['tmp_name'], $dir.'/'.$before.'.po');

$Vars->message(__('Language file was updated successfully.'), 'ok');
?>
